package com.wang.web3;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wang.web3.adapter.FullyGridLayoutManager;
import com.wang.web3.adapter.KittyGrilAdapter;
import com.wang.web3.adapter.PropsAdapter;
import com.wang.web3.base.Constant;
import com.wang.web3.bean.GirlBean;
import com.wang.web3.bean.KittyInfoBean;
import com.wang.web3.bean.PropsBean;
import com.wang.web3.bean.TianWallet;
import com.wang.web3.bean.UserInfoBean;
import com.wang.web3.contracts.KittyCore;
import com.wang.web3.contracts.PhotoFrame;
import com.wang.web3.contracts.RandomBox;
import com.wang.web3.contracts.SaleClockAuction;
import com.wang.web3.contracts.Wing;
import com.wang.web3.data.service.ICallBackListener;
import com.wang.web3.data.service.impl.ApiServiceImpl;
import com.wang.web3.utils.CalendarUtils;
import com.wang.web3.utils.SPUtils;
import com.wang.web3.weight.KittyWindow;
import com.wang.web3.weight.PropsWindow;

import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jFactory;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tuples.generated.Tuple11;
import org.web3j.tuples.generated.Tuple2;
import org.web3j.tuples.generated.Tuple4;
import org.web3j.tuples.generated.Tuple5;
import org.web3j.tx.Contract;
import org.web3j.utils.Convert;

import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import rx.functions.Action1;

import static com.wang.web3.base.Constant.URL;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link BackpackFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BackpackFragment extends Fragment implements KittyWindow.KittyWindowOnClick, PropsWindow.PropsWindowOnClick, KittyGrilAdapter.PersonHolderListener, PropsAdapter.PersonHolderListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    Button mCreatBtn;
    TextView tvCreatInfo, tvnickname;

    Web3j web3;
    Credentials credentials;
    KittyCore kittyCore;
    RandomBox randomBox;
    Wing wing;
    PhotoFrame photoFrame;
    SaleClockAuction saleClockAuction;

    RecyclerView mKittyRv, mPropsRv;
    List<KittyInfoBean> list = new ArrayList<>();
    KittyWindow kittyWindow;
    PropsWindow propsWindow;
    Gson gson = new Gson();
    KittyGrilAdapter kittyGrilAdapter;
    PropsAdapter propsAdapter;
    List<GirlBean.DataBean> girlLists;
    List<PropsBean.DataBean> propsLists;

    String kittyCurrentId = "";
    int propsType = 0;

    public BackpackFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BackpackFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BackpackFragment newInstance(String param1, String param2) {
        BackpackFragment fragment = new BackpackFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }


    private MyHandler handler = new MyHandler(this);

    @Override
    public void onPersonItemClicked(View v, MotionEvent event, int position) {
        Intent i = new Intent(getContext(), MyPropsDetailActivity.class);
        i.putExtra("girl_img", propsLists.get(position).getItemPic());
        i.putExtra("girl_id", propsLists.get(position).getCategoryName());
        startActivity(i);
    }

    @Override
    public void onGirlPersonItemClicked(View v, MotionEvent event, int position) {
        Intent i = new Intent(getContext(), MyGirlsDetailActivity.class);
        i.putExtra("girl_img", girlLists.get(position).getPicBody());
        i.putExtra("girl_id", girlLists.get(position).getId());
        i.putExtra("girlB", girlLists.get(position));
        startActivity(i);
    }

    private static class MyHandler extends Handler {
        private WeakReference<BackpackFragment> reference;

        public MyHandler(BackpackFragment context) {
            reference = new WeakReference<BackpackFragment>(context);
        }

        @Override
        public void handleMessage(Message msg) {
            BackpackFragment backpackFragment = reference.get();
            switch (msg.what) {
                //现有道具
                case 0:
                    backpackFragment.tvCreatInfo.setText((String) msg.obj);
                    try {
                        backpackFragment.getKittiesCount();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;
                //开箱结果
                case 1:
                    break;
                //钥匙数量
                case 2:
                    break;
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_backpack, container, false);
        mCreatBtn = view.findViewById(R.id.mCreatBtn);
        mKittyRv = view.findViewById(R.id.mKittyRv);
        mPropsRv = view.findViewById(R.id.mPropsRv);
        tvnickname = view.findViewById(R.id.nickname);
        view.findViewById(R.id.mtranBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), TransactionActivity.class));
            }
        });

        view.findViewById(R.id.mStrongBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), GirlStrongActivity.class));
            }
        });

        view.findViewById(R.id.mSummonBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), GirlSummonActivity.class));
            }
        });
        view.findViewById(R.id.mBreakDownBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), GirlBreakDownActivity.class));
            }
        });


        try {
            init();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        initData();
        return view;
    }

    private void initData() {
        String lockWallet = (String) SPUtils.get(getActivity(), Constant.WALLETLOCKADDRESS, "");
        if (lockWallet.equals("")) {
            return;
        }
        TianWallet tianWallet = gson.fromJson(lockWallet, TianWallet.class);
        ApiServiceImpl.getInstence().userInfo(tianWallet.getAdress(), new ICallBackListener() {
            @Override
            public void onSuccess(String string) {
                UserInfoBean userInfoBean = gson.fromJson(string, UserInfoBean.class);
                tvnickname.setText(userInfoBean.getData().getNickname());
            }

            @Override
            public void onFaild(String string) {

            }
        });

        ApiServiceImpl.getInstence().myGirlList(tianWallet.getAdress(), new ICallBackListener() {
            @Override
            public void onSuccess(String string) {
                GirlBean girlBean = gson.fromJson(string, GirlBean.class);
                girlLists.addAll(girlBean.getData());
                kittyGrilAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFaild(String string) {

            }
        });

        ApiServiceImpl.getInstence().myPropsList(tianWallet.getAdress(), new ICallBackListener() {
            @Override
            public void onSuccess(String string) {
                PropsBean propsBean = gson.fromJson(string, PropsBean.class);
                propsLists.addAll(propsBean.getData());
                propsAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFaild(String string) {

            }
        });
    }

    //初始化链接
    private void init() throws ExecutionException, InterruptedException {

        web3 = Web3jFactory.build(new HttpService(URL));
        credentials = Credentials.create(Constant.ownKey);
        kittyCore = KittyCore.load(Constant.KittyCoreAddress, web3, credentials, Contract.GAS_PRICE, Contract.GAS_LIMIT);
        randomBox = RandomBox.load(Constant.RandomBoxAddress, web3, credentials, Constant.GAS_PRICE, Constant.GAS_LIMIT);
        wing = Wing.load(Constant.WingAddress, web3, credentials, Constant.GAS_PRICE, Constant.GAS_LIMIT);
        photoFrame = PhotoFrame.load(Constant.PhotoFrameAddress, web3, credentials, Constant.GAS_PRICE, Constant.GAS_LIMIT);
        saleClockAuction = SaleClockAuction.load(Constant.SaleClockAuctionAddress, web3, credentials, Constant.GAS_PRICE, Constant.GAS_LIMIT);
//        kittyBase = KittyBase.load(KitttyCoreAddress,web3,credentials, Contract.GAS_PRICE,Contract.GAS_LIMIT);
//        getKittiesCount();

        girlLists = new ArrayList<>();
        kittyGrilAdapter = new KittyGrilAdapter(girlLists, this);
        mKittyRv.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        mKittyRv.setAdapter(kittyGrilAdapter);

        propsLists = new ArrayList<>();
        propsAdapter = new PropsAdapter(propsLists, new PropsAdapter.PersonHolderListener() {
            @Override
            public void onPersonItemClicked(View v, MotionEvent event, int position) {
                Intent i = new Intent(getContext(), PropsDetailActivity.class);
                i.putExtra("propId", propsLists.get(position).getId());
                i.putExtra("categoryName", propsLists.get(position).getCategoryName());
                i.putExtra("itempic", propsLists.get(position).getItemPic());
                startActivity(i);
            }
        });
        mPropsRv.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        mPropsRv.setAdapter(propsAdapter);


    }

    //弹出道具框
    public void propsInfo(int type, String info) {
//        propsType = type;
//        PropsBean propsBean = new PropsBean(type,info);
//        propsWindow = PropsWindow.show(getFragmentManager(),propsBean);
//        propsWindow.initOnClick(this);
    }

    //创建并赠送少女
    private void createKitty() {
        BigInteger bigInteger = new BigInteger("123");
        final RemoteCall<TransactionReceipt> rm = kittyCore.createPromoKitty(bigInteger, Constant.ownAddress);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    rm.observable().subscribe(new Action1<Object>() {
                        @Override
                        public void call(Object o) {
                            TransactionReceipt ss = (TransactionReceipt) o;
                            List<KittyCore.BirthEventResponse> list = kittyCore.getBirthEvents(ss);
                            KittyCore.BirthEventResponse birthEventResponse = list.get(0);
                            String obj = "genes==" + birthEventResponse.genes + "\n kitty id===" + birthEventResponse.kittyId +
                                    "\n matronId == " + birthEventResponse.matronId + "\n owner == " + birthEventResponse.owner
                                    + "\n sireid == " + birthEventResponse.sireId;
                            Message message = Message.obtain();
                            message.what = 0;
                            message.obj = obj;
                            handler.sendMessage(message);
                            String hosity = (String) SPUtils.get(getActivity(), Constant.KITTY_ID, "");
                            SPUtils.put(getActivity(), Constant.KITTY_ID, hosity + "--" + birthEventResponse.kittyId);
//                            Log.e("Tag","obj" + obj);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void summonGirl() {
        try {
            TransactionReceipt receipt = kittyCore.summonGirl(new BigInteger("4"), new BigInteger("5"), new BigInteger("123")).sendAsync().get();
            List<KittyCore.BirthEventResponse> list = kittyCore.getBirthEvents(receipt);
            if (receipt.getStatus().equals("1")) {
                Toast.makeText(getContext(), "召唤成功", Toast.LENGTH_SHORT).show();
                getKittiesCount();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }


    //加密猫信息
    public void getKittyInfo(String kittyId, boolean isUpdata) throws ExecutionException, InterruptedException {
        BigInteger bigInteger = new BigInteger(kittyId);
        Tuple11<Boolean, Boolean, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger> tuple11 = kittyCore.getKitty(bigInteger).sendAsync().get();
        String info1 = "isGestating:" + tuple11.getValue1() +
                "\n isReady:" + tuple11.getValue2() +
                "\n cooldownIndex:" + tuple11.getValue3() +
                "\n nextActionAt:" + tuple11.getValue4() +
                "\n siringWithId:" + tuple11.getValue5() +
                "\n birthTime:" + CalendarUtils.getStartStringTime(tuple11.getValue6().longValue(), "yyyy-MM-dd");

        String info2 = "matronId:" + tuple11.getValue7() +
                "\n sireId:" + tuple11.getValue8() +
                "\n generation:" + tuple11.getValue9() +
                "\n genes:" + tuple11.getValue10() +
                "\n level:" + tuple11.getValue11();

       /* uint8 wingType;
        uint8 photoFrameType;
        uint256 wingTime;
        uint256 photoFrameTime;*/
        Tuple4<BigInteger, BigInteger, BigInteger, BigInteger> tuple4 = randomBox.kittyIndexToDemoEquipement(bigInteger).sendAsync().get();
        String info3 = "wingType:" + tuple4.getValue1() +
                "\n wingTime:" + CalendarUtils.getStartStringTime(tuple4.getValue3().longValue(), "yyyy-MM-dd hh:mm:ss") +
                "\n photoFrameType:" + tuple4.getValue2() +
                "\n photoFrameTime:" + CalendarUtils.getStartStringTime(tuple4.getValue4().longValue(), "yyyy-MM-dd hh:mm:ss");

        boolean wings = false;
        boolean photo = false;
        if (tuple4.getValue1().intValue() == 1) {
            wings = true;
        }
        if (tuple4.getValue2().intValue() == 1) {
            photo = true;
        }

        if (isUpdata) {
            kittyWindow.upDateInfo(info1, info2, info3);
            kittyWindow.haveProps(wings, photo);
        } else {
            kittyWindow = KittyWindow.showKitty(getChildFragmentManager(), info1, info2, info3);
            kittyWindow.initOnClick(this);
        }

    }

    //获取该账号下的少女
    private void getKittiesCount() throws ExecutionException, InterruptedException {
        final RemoteCall<BigInteger> rm = kittyCore.getKittiesCount();
        BigInteger bigInteger = rm.sendAsync().get();
        BigInteger bigInteger1 = kittyCore.totalSupply().sendAsync().get();

        Tuple2<BigInteger, BigInteger> tuple2 = randomBox.getPropsCount(Constant.ownAddress).sendAsync().get();

        for (int i = 0; i < bigInteger1.intValue(); i++) {
            RemoteCall<String> remoteCall = kittyCore.kittyIndexToOwner(new BigInteger((i + 1) + ""));
            String test = remoteCall.sendAsync().get();
            if (Constant.ownAddress.equalsIgnoreCase(test)) {
                Tuple4<BigInteger, BigInteger, BigInteger, BigInteger> tuple4 = randomBox.kittyIndexToDemoEquipement(new BigInteger((i + 1) + "")).sendAsync().get();
                list.add(new KittyInfoBean(i + 1, tuple4));
                Log.e("wzz", "---" + i);
            }
        }
//        MarketGrilAdapter kittyGrilAdapter = new  MarketGrilAdapter(list, new MarketGrilAdapter.PersonHolderListener() {
//            @Override
//            public void onPersonItemClicked(View v, MotionEvent event, int position) {
//                try {
//                    kittyCurrentId = "" + list.get(position).getId();
//                    getKittyInfo(kittyCurrentId,false);
//                } catch (ExecutionException e) {
//                    e.printStackTrace();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
        mKittyRv.setLayoutManager(new FullyGridLayoutManager(getContext(), 2));
//        mKittyRv.setAdapter(kittyGrilAdapter);
        Log.e("wzz", "---" + list.size());


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mBtnVestWings:
                vestWing(kittyCurrentId);
                break;
            case R.id.mBtnVestPhotoFrame:
                vestPhotoFrame(kittyCurrentId);
                break;
            case R.id.mBtnResolveKitty:
                sendResolveKitty(kittyCurrentId);
                break;
            case R.id.mBtnBackKitty:
                sendBackKetty(kittyCurrentId);
                break;
            case R.id.mBtnCreateSale:
                createSale();
                break;
            case R.id.mBtnCreateBuy:
                createBuy();
                break;
            case R.id.mBtnSaleKitty:
                saleKitty();
                break;
            case R.id.mBtnBuyKitty:
                buyKitty();
                break;
        }
    }

    private void buyKitty() {

        BigInteger bigInteger = new BigInteger("" + kittyCurrentId);
        try {
            /*            auction.seller,
            auction.startingPrice,
            auction.endingPrice,
            auction.duration,
            auction.startedAt*/
            Tuple5<String, BigInteger, BigInteger, BigInteger, BigInteger> tuple5 = saleClockAuction.getAuction(bigInteger).sendAsync().get();
            Log.e("Tag", "===" + tuple5.getValue1());
//            (当前时间-开始时间) *（(结束价格 - 开始价格) / 持续时间） + 开始价格
            long bigInteger1 = (int) (CalendarUtils.getCurrentTimes() / 1000 - tuple5.getValue5().longValue()) * (tuple5.getValue3().subtract(tuple5.getValue2()).divide(tuple5.getValue4()).longValue()) + tuple5.getValue2().intValue();
            TransactionReceipt receipt = saleClockAuction.bid(bigInteger, new BigInteger(bigInteger1 + "")).sendAsync().get();
            List<org.web3j.protocol.core.methods.response.Log> loglist = receipt.getLogs();
            if (receipt.getStatus().equals("1")) {
                Toast.makeText(getContext(), "购买成功", Toast.LENGTH_SHORT).show();
            }
            for (int i = 0; i < loglist.size(); i++) {
                org.web3j.protocol.core.methods.response.Log log = loglist.get(i);
                Log.e("Tag", "---" + log.toString());
            }
            Log.e("Tag", "---");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            try {
                getKittiesCount();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //上架少女
    private void saleKitty() {
        BigInteger bigInteger = new BigInteger(kittyCurrentId);
        BigInteger startPrice = Convert.toWei("1.0", Convert.Unit.ETHER).toBigInteger();
        BigInteger endPrice = Convert.toWei("5.0", Convert.Unit.ETHER).toBigInteger();
        BigInteger duaction = new BigInteger("7200");

        try {
            TransactionReceipt receipt = kittyCore.createSaleAuction(bigInteger, startPrice, endPrice, duaction).sendAsync().get();
            List<org.web3j.protocol.core.methods.response.Log> loglist = receipt.getLogs();
            for (int i = 0; i < loglist.size(); i++) {
                org.web3j.protocol.core.methods.response.Log log = loglist.get(i);
                Log.e("Tag", "---" + log.toString());
            }
            if (receipt.getStatus().equals("1")) {
                Toast.makeText(getContext(), "上架成功", Toast.LENGTH_SHORT).show();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    //创建购买
    private void createBuy() {

        Credentials credentials2 = Credentials.create(Constant.duoKey);
        Wing wing2 = Wing.load(Constant.WingAddress, web3, credentials2, Constant.GAS_PRICE, Constant.GAS_LIMIT);
        PhotoFrame photoFrame2 = PhotoFrame.load(Constant.PhotoFrameAddress, web3, credentials2, Constant.GAS_PRICE, Constant.GAS_LIMIT);

        String seller = "0x627306090abab3a6e1400e9345bc60c78a8bef57";
        BigInteger type = new BigInteger("" + propsType);
        BigInteger count = new BigInteger("1");
        BigInteger value = Convert.toWei("1.0", Convert.Unit.ETHER).toBigInteger();
        try {
            TransactionReceipt receipt;
            if (propsType == 0) {
                receipt = wing2.buy(seller, type, count, value).sendAsync().get();
            } else {
                receipt = photoFrame2.buy(seller, type, count, value).sendAsync().get();
            }
            Log.e("tag", "---");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
            Toast.makeText(getActivity(), "text" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    //道具上架
    private void createSale() {
        BigInteger bigInteger = new BigInteger("" + propsType);
        BigInteger price = Convert.toWei("1.0", Convert.Unit.ETHER).toBigInteger();
        BigInteger count = new BigInteger("1");
        try {
            TransactionReceipt receipt;
            if (propsType == 0) {
//                Tuple2<BigInteger, BigInteger> tou = wing.ownerToEquipmentAuction(Constant.ownAddress).sendAsync().get();
                receipt = wing.createAuction(bigInteger, price, count).sendAsync().get();
                List<Wing.AquipmentAuctionCreatedEventResponse> wingResponse = (List<Wing.AquipmentAuctionCreatedEventResponse>) wing.getAquipmentAuctionCreatedEvents(receipt);
                Log.e("tag", "====");
            } else {
                receipt = photoFrame.createAuction(bigInteger, price, count).sendAsync().get();
                List<PhotoFrame.AquipmentAuctionCreatedEventResponse> wingResponses = (List<PhotoFrame.AquipmentAuctionCreatedEventResponse>) photoFrame.getAquipmentAuctionCreatedEvents(receipt);
                Log.e("tag", "====");
            }
            if (receipt.getStatus().equals("1")) {
                Toast.makeText(getContext(), "上架成功", Toast.LENGTH_SHORT).show();
                Tuple2<BigInteger, BigInteger> tuple2 = randomBox.getPropsCount(Constant.ownAddress).sendAsync().get();
                if (propsType == 0) {
                    propsWindow.upDate("翅膀:" + tuple2.getValue1());
                } else {
                    propsWindow.upDate("相框:" + tuple2.getValue2());
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }


    //穿戴翅膀
    public void vestWing(String kittyId) {
        BigInteger bigInteger = new BigInteger(kittyId);
        BigInteger bigInteger1 = new BigInteger("1");
        try {
            TransactionReceipt receipt = randomBox.vestWing(bigInteger, bigInteger1).sendAsync().get();
            if (receipt.getStatus().equals("1")) {
                Toast.makeText(getContext(), "穿戴成功", Toast.LENGTH_SHORT).show();
                getKittyInfo(kittyId, true);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    //穿戴相框
    public void vestPhotoFrame(String kittyId) {
        BigInteger bigInteger = new BigInteger(kittyId);
        BigInteger bigInteger1 = new BigInteger("1");
        try {
            TransactionReceipt receipt = randomBox.vestPhotoFrame(bigInteger, bigInteger1).sendAsync().get();
            if (receipt.getStatus().equals("1")) {
                Toast.makeText(getContext(), "穿戴成功", Toast.LENGTH_SHORT).show();
                getKittyInfo(kittyId, true);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    //分解少女
    public void sendResolveKitty(String kittyId) {
        BigInteger bigInteger = new BigInteger(kittyId);
        try {
            TransactionReceipt receipt = randomBox.sendResolveKitty(bigInteger).sendAsync().get();
            if (receipt.getStatus().equals("1")) {
                Toast.makeText(getContext(), "分解成功", Toast.LENGTH_SHORT).show();
                kittyWindow.dismiss();
                getKittiesCount();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    //回购少女
    public void sendBackKetty(String kittyId) {
        BigInteger bigInteger = new BigInteger(kittyId);
        try {
            TransactionReceipt receipt = randomBox.sendBackKetty(bigInteger).sendAsync().get();
            if (receipt.getStatus().equals("1")) {
                Toast.makeText(getContext(), "回购成功", Toast.LENGTH_SHORT).show();
                kittyWindow.dismiss();
                getKittiesCount();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}
